#include "fuserswin.h"
#include "ui_fuserswin.h"
#include"QVBoxLayout"
#include"users.h"
#include"part1.h"
#include <QDebug>
#include<QMessageBox>
fuserswin::fuserswin(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::fuserswin)
{
    ui->setupUi(this);
}

fuserswin::~fuserswin()
{
    delete ui;
}
 void fuserswin::on_fineButton_clicked()
    {    QString a=ui->finformline->text();
        QByteArray a1=a.toUtf8();
        char*a2=a1.data();
        const char*file_name="users.txt";
        users*b=loadUsersFromFile(file_name);
        users*c=NULL;
        QString cho=ui->choiceBox->currentText();
        if(cho=="用户名查找"){
            c=findUserByUsername(b,a2);
            qDebug()<<c;
            if(c==NULL){
                ui->showcondition->setText("用户名查找无结果");
            }else {
                na=QString::fromUtf8(c->username);
                QString pa=QString::fromUtf8(c->password);
                QString ph=QString::fromUtf8(c->phonenumber);
                ui->showcondition->append("用户名:");
                ui->showcondition->append(na);
                ui->showcondition->append("密码:");
                ui->showcondition->append(pa);
                ui->showcondition->append("电话号码:");
                ui->showcondition->append(ph);
                ui->showcondition->append("身份:");
                if(c->type==1){
                    ui->showcondition->append("管理员");
                }else if(c->type==2){
                    ui->showcondition->append("员工");
                }else if(c->type==3){
                    ui->showcondition->append("用户");
                }
                ui->showcondition->append("会员等级:");
                ui->showcondition->append(QString::number(c->members.level));
                ui->showcondition->append("会员积分:");
                ui->showcondition->append(QString::number(c->members.points));
            }
        }else{
            c=findUserByPhoneNumber(b,a2);
            if(c==NULL){
                ui->showcondition->setText("电话号码查询无结果");
            }else{
                 na=QString::fromUtf8(c->username);
                QString pa=QString::fromUtf8(c->password);
                QString ph=QString::fromUtf8(c->phonenumber);
                ui->showcondition->append("用户名:");
                ui->showcondition->append(na);
                ui->showcondition->append("密码:");
                ui->showcondition->append(pa);
                ui->showcondition->append("电话号码:");
                ui->showcondition->append(ph);
                ui->showcondition->append("身份:");
                if(c->type==1){
                    ui->showcondition->append("管理员");
                }else if(c->type==2){
                    ui->showcondition->append("员工");
                }else if(c->type==3){
                    ui->showcondition->append("用户");
                }
                ui->showcondition->append("会员等级:");
                ui->showcondition->append(QString::number(c->members.level));
                ui->showcondition->append("会员积分:");
                ui->showcondition->append(QString::number(c->members.points));
            }
        }

    }


    void fuserswin::on_choiceBox_currentTextChanged(const QString &arg1)
    {

    }


    void fuserswin::on_returnButton_clicked()
    {
        this->close();
    }


    void fuserswin::on_modButton_clicked()
    {
        QByteArray te=na.toUtf8();
        char* tem=te.data();//名字
        QString ne;
        QByteArray nee;
        int choice=ui->choicebox2->currentIndex();
        switch (choice) {
        case 3: // 类型需要数值转换
        case 5: // 会员等级
        case 6: { // 会员积分
            int abc=ui->modline->text().toInt();
            nee=QByteArray::number(abc);
            break;
        } default:
            ne=ui->modline->text();
            nee=ne.toUtf8();
        }
            char *x=nee.data();//新信息
        Users*head=loadUsersFromFile("users.txt");
        Users* result = modifyUsersMembers( head,  tem,  choice,  x );
        if (result) {
            QMessageBox::information(this, "Success", "修改成功");
            ui->finformline->clear();
        } else {
            QMessageBox::warning(this, "Error", "修改失败：无效输入或用户不存在");
        }
    }


    void fuserswin::on_deleButton_clicked()
    {
        QByteArray usernameBytes=ui->deleteline->text().toUtf8();
         const char* targetUsername = usernameBytes.data();
        Users*userList=loadUsersFromFile("users.txt");
         int success = deleteUser(&userList, targetUsername);
         if (success) {
             // 4. 保存修改后的链表
             if (saveListToFile(userList, "users.txt")) {
                 QMessageBox::information(this, "成功", "用户删除成功");
             } else {
                 QMessageBox::critical(this, "错误", "文件保存失败");
             }
         } else {
             QMessageBox::warning(this, "错误", "用户不存在或删除失败");
         }

    }

